﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LWrap.IndexSource;
namespace LWrap.Index
{
    public class IndexJob
    {
        /// <summary>
        /// Folder path to persist the index files
        /// </summary>
        public string IndexFilePath { get; set; }

        /// <summary>
        /// True: To create new index folder, recreate the folder if exist, hence all index files will get deleted
        /// False: To append data.
        /// </summary>
        public bool CreateNewIndex { get; set; }

        /// <summary>
        /// Assign the IndexSource object. 
        /// Available IndexSources: 
        /// DBDataSource: Index data base. User should assign DataTable
        /// FileDataSource: Index a specified folder: User should assign folder path.
        /// NameSpace: LWrap.IndexSource
        /// </summary>
        public IndexDataSource IndexSource { get; set; }

        public IndexJob()
        {
            this.CreateNewIndex = false;
        }

        /// <summary>
        /// The method will initiate the indexing process. IndexSource should not be null.
        /// Available IndexSource: DBDataSource and FileDataSource. 
        /// NameSpace: LWrap.IndexSource
        /// </summary>
        public void ExecuteIndex()
        {
            if (IndexSource == null)
            {
                throw new NullReferenceException("Can't index data. IndexSource should not be null");
            }
            IndexingMonitor.InitiateIndex(IndexSource, IndexFilePath, CreateNewIndex);

        }

        /// <summary>
        /// Delete the document that satisfies the boolean condition. Before calling this function
        /// the user should set the IndexFilePath property.
        /// </summary>
        /// <param name="booleanCondition"></param>
        public void DeleteDocuments(string booleanCondition)
        {
            if (string.IsNullOrEmpty(this.IndexFilePath) == true)
                throw new NullReferenceException("IndexFilePath should not be null");
            this.CreateNewIndex = false;
            IndexWriter indexWriter = new IndexWriter(IndexFilePath, CreateNewIndex);
            indexWriter.DeleteDocument(booleanCondition);

            indexWriter.Dispose();
        }

        /// <summary>
        /// Method will return true if the Index Files exist in the specified folder else false.
        /// Based on the return value user can decide what value he should set to CreateNewIndex property
        /// </summary>
        /// <param name="indexPath">Folder path for indexing files</param>
        /// <returns>True: Index Files exist False:Index Files not exist</returns>
        public bool IsIndexFilesExist(string indexPath)
        {
            bool isExist = false;
            if (Directory.Exists(indexPath))
            {
                System.String[] files = System.IO.Directory.GetFileSystemEntries(indexPath);
                if (files.Length > 0)
                    isExist = true;
            }
            else
                isExist = false;

            return isExist;
        }
    }
}
